'''

Case Type： stat文件
Case Name： stat文件丢失
Create At:  2020/07/01
Owner    :  n@ningyali
Descption:  1.执行sql并行脚本，正常执行 2.删除stat文件 3.执行sql并行脚本，正常执行 4.还原

'''

import os
import sys
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class StatLost(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case092.py start------------')
        self.stat_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.STATE_FILE_NAME)
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.com = Common()
        self.sqlfile1 = 'bx1.sql'
        self.sqlfile2 = 'bx2.sql'

        # 备份state文件
        bak_cmd = f'cp {self.stat_file_path} {self.stat_file_path}.baktest;'
        self.log.info(bak_cmd)
        msg = self.dbUserNode.sh(bak_cmd).result()
        self.log.info(msg)
        self.assertEqual(msg, '')

    def test_stat_lost(self):
        # 目标主机新建路径
        self.to_path = os.path.dirname(macro.DB_INSTANCE_PATH) + '/test_script/'
        # 目标主机文件路径
        self.newsql_path1 = os.path.join(self.to_path, self.sqlfile1)
        self.newsql_path2 = os.path.join(self.to_path, self.sqlfile2)
        # 拷贝文件
        self.com.scp_file(self.rootNode, self.sqlfile1, self.to_path)
        self.com.scp_file(self.rootNode, self.sqlfile2, self.to_path)

        # 执行sql并行脚本，正常执行
        self.log.info('----------------------------执行sql并行脚本-----------------------------')
        sql_bx_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path1} &
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path2} &
                    wait
                    '''
        self.log.info(sql_bx_cmd)
        sql_bx_msg = self.dbUserNode.sh(sql_bx_cmd).result()
        self.log.info(sql_bx_msg)
        self.assertIn('total time: ', sql_bx_msg)

        # 删除stat文件
        self.log.info('----------------------------删除stat文件-----------------------------')
        del_cmd = f'rm -rf {self.stat_file_path}'
        self.log.info(del_cmd)
        msg = self.dbUserNode.sh(del_cmd).result()
        self.log.info(msg)
        self.assertEqual(msg, '')

        # 执行sql并行脚本，正常执行
        self.log.info('----------------------------执行sql并行脚本-----------------------------')
        sql_bx_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path1} &
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path2} &
                    wait
                    '''
        self.log.info(sql_bx_cmd)
        sql_bx_msg = self.dbUserNode.sh(sql_bx_cmd).result()
        self.log.info(sql_bx_msg)
        self.assertIn('total time: ', sql_bx_msg)

    def tearDown(self):
        # 还原
        self.log.info('----------------------------还原stat文件-----------------------------')
        recover_cmd = f'mv {self.stat_file_path}.baktest {self.stat_file_path}'
        self.log.info(recover_cmd)
        msg = self.dbUserNode.sh(recover_cmd).result()
        self.log.info(msg)
        self.assertEqual(msg, '')
        self.log.info('------------------Opengauss_Reliability_Dbsys_Case092:用例执行完毕--------------------')
